package com.lg.erp.service.impl.yqy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lg.erp.entity.SCompany;
import com.lg.erp.entity.yqy.TCategories;
import com.lg.erp.entity.yqy.TProduct;
import com.lg.erp.mapper.SCompanyMapper;
import com.lg.erp.mapper.yqy.TCategoriesMapper;
import com.lg.erp.mapper.yqy.TProductMapper;
import com.lg.erp.service.yqy.ITProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiu
 * @since 2021-04-19
 */
@Service
public class TProductServiceImpl extends ServiceImpl<TProductMapper, TProduct> implements ITProductService {

    @Autowired
    private TProductMapper productMapper;

    @Autowired
    private TCategoriesMapper categoriesMapper;

    @Autowired
    private SCompanyMapper companyMapper;

    @Override
    public IPage<TProduct> selectPageProducts(IPage page) {
        IPage iPage = productMapper.selectPage(page, null);
        return iPage;
    }

    @Override
    public IPage<TProduct> selectPageProductByName(IPage page, String name, String enterprise) {
        IPage<TProduct> iPage = productMapper.selectPageProductByName(page, name, enterprise);
        List<TProduct> list = iPage.getRecords();
        for (TProduct tProduct : list) {
            System.out.println(tProduct.getProductEnterprise());
            TCategories tCategories = categoriesMapper.selectOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getId,tProduct.getProductEnterprise()));
            SCompany sCompany = companyMapper.selectById(tProduct.getCompanyId());
            tProduct.setCategories(tCategories);
            tProduct.setCompany(sCompany);
        }
        return iPage;
    }

    @Override
    public IPage<TProduct> selectProductByApply(IPage page) {
        IPage<TProduct> iPage = productMapper.selectProductByApply(page);
        List<TProduct> list = iPage.getRecords();
        for (TProduct tProduct : list) {
            System.out.println(tProduct.getProductEnterprise());
            TCategories tCategories = categoriesMapper.selectOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getId,tProduct.getProductEnterprise()));
            SCompany sCompany = companyMapper.selectById(tProduct.getCompanyId());
            tProduct.setCategories(tCategories);
            tProduct.setCompany(sCompany);
        }
        return iPage;
    }

    @Override
    public TProduct selectProductById2(String id) {
        TProduct tProduct = productMapper.selectById(id);
        TCategories tCategories = categoriesMapper.selectOne(new QueryWrapper<TCategories>().lambda().eq(TCategories::getId,tProduct.getProductEnterprise()));
        tProduct.setCategories(tCategories);
        return tProduct;
    }

    @Override
    public TProduct getProductById(String id) {
        return productMapper.selectProductById(id);
    }
}
